package net.kem.android.positioner;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class POIActivity extends Activity {
	public final static String EXTRA_MESSAGE_POI = "POI";
	private PositionerService.LocalBinder binder;
	
	private EditText etName;
	private EditText etNotes;
	private EditText etLat;
	private EditText etLon;
	private EditText etAlt;
//	private EditText etAccuracy;
	private EditText etRadius;
	private EditText etID;
	private EditText etType;
	private Button buAdd;
	private Button buDelete;
	private Button buUpdate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.poi);
        etName = (EditText)findViewById(R.id.settingsPoiName);
        etNotes = (EditText)findViewById(R.id.settingsPoiNotes);
        etLat = (EditText)findViewById(R.id.settingsPoiLatitude);
        etLon = (EditText)findViewById(R.id.settingsPoiLongitude);
        etAlt = (EditText)findViewById(R.id.settingsPoiAltitude);
//        etAccuracy = (EditText)findViewById(R.id.settingsPoiAccuracy);
        etRadius = (EditText)findViewById(R.id.settingsPoiRadius);
        etID = (EditText)findViewById(R.id.settingsPoiID);
        etType = (EditText)findViewById(R.id.settingsPoiType);
        buAdd = (Button)findViewById(R.id.settingsButtonPOIAdd);
        buDelete = (Button)findViewById(R.id.settingsButtonPOIDelete);
        buUpdate = (Button)findViewById(R.id.settingsButtonPOIUpdate);
        
        Intent intent = getIntent();
        POI poi = intent.getExtras().getParcelable(EXTRA_MESSAGE_POI);
        if(poi != null) {
        	etName.setText(poi.getName());
        	etNotes.setText(poi.getNotes());
        	etLat.setText(String.valueOf(poi.getCoordinates().getLatitude()));
        	etLon.setText(String.valueOf(poi.getCoordinates().getLongitude()));
        	etAlt.setText(String.valueOf(poi.getCoordinates().getAltitude()));
//        	etAccuracy.setText(String.valueOf(poi.getAccuracy()));
        	etRadius.setText(String.valueOf(poi.getRadius()));
        	etID.setText(String.valueOf(poi.getId()));
        	etType.setText(String.valueOf(poi.getType()));
        	
        	buAdd.setOnClickListener(new OnClickListener() {
    			public void onClick(View view) {
    				POI poi = new POI(Integer.parseInt(etID.getText().toString()), Integer.parseInt(etType.getText().toString())
    						, etName.getText().toString()
    						, Double.parseDouble(etLat.getText().toString()), Double.parseDouble(etLon.getText().toString()), Double.parseDouble(etAlt.getText().toString())
    						, Float.parseFloat("0.0f"/*etAccuracy.getText().toString()*/), Integer.parseInt(etRadius.getText().toString()), etNotes.getText().toString());
    				poi.store();
    				binder.sendAddPOI(poi);
    				setResult(RESULT_OK);
    				finish();
    			}
    		});
        	buDelete.setOnClickListener(new OnClickListener() {
    			public void onClick(View view) {
    				POI poi = Settings.getPOIById(Integer.parseInt(etID.getText().toString()));
    				if(poi != null) {
    					POI.delete(poi);
    					binder.sendDeletePOI(poi);
    				}
    				setResult(RESULT_CANCELED);
    				finish();
    			}
    		});
        	buUpdate.setOnClickListener(new OnClickListener() {
        		public void onClick(View view) {
        			POI poi = Settings.getPOIById(Integer.parseInt(etID.getText().toString()));
        			if(poi != null) {
        				poi.setAccuracy(Float.parseFloat("0.0f"/*etAccuracy.getText().toString()*/));
        				Location location = poi.getCoordinates();
        				location.setAltitude(Double.parseDouble(etAlt.getText().toString()));
        				location.setLatitude(Double.parseDouble(etLat.getText().toString()));
        				location.setLongitude(Double.parseDouble(etLon.getText().toString()));
        				poi.setName(etName.getText().toString());
        				poi.setNotes(etNotes.getText().toString());
        				poi.setRadius(Integer.parseInt(etRadius.getText().toString()));
        				poi.setType(Integer.parseInt(etType.getText().toString()));
        				poi.store();
        			}
        			binder.sendUpdatePOI(poi);
        			setResult(RESULT_OK);
        			finish();
        		}
        	});
        }
        
        // Bind to PositionerService
     	Intent bindIntent = new Intent(this, PositionerService.class);
     	bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);
    }
    
    /** Defines callbacks for service binding, passed to bindService() */
	private ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName className, IBinder service) {
			binder = (PositionerService.LocalBinder) service;
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			binder = null;
		}
	};
}
